package com.daxue.util.day01;

import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * linux环境下绝对路径转map
 * dependency:
 *  com.google.code.gson:gson:2.3
 * @author daxue0929
 * @date 2022/10/31
 */

public class PathToMapTest {

    @Test
    public void main() throws UnsupportedEncodingException {
//        List<String> pathList = Arrays.asList(
//            "/etc/hosts",
//            "/etc/kubernetes/ssl/certs",
//            "/root",
//            "/root/document/daxue/2022"
//        );
//        Map<String, Map> mapTree = pathListToMap(pathList);
//        GsonBuilder builder = new GsonBuilder();
//        String result = builder.setPrettyPrinting()
//            .create()
//            .toJson(mapTree);
//        System.out.println(result);



        String gotoUri = URLDecoder.decode("6CuXh9wgUzVk4/jeJw4c9BZhGAuqAy2m2lwDAQ0Bm/LZqAFqJdgHutNrImnpg+485nNnH7aui2MnALwMlyxU6Q==", "UTF-8");
        System.out.println(gotoUri);
    }

    public Map<String, Map> pathListToMap(List<String> pathList) {
        List<String[]> array = pathList.stream()
            .map(each -> each.split("/"))
            .collect(Collectors.toList());
        HashMap<String, Map> result = new HashMap<>();
        for (int i = 0; i < array.size(); i++) {
            Map<String, Map> currentMap = result;
            String[] pathArray = array.get(i);
            for (int j = 1; j < pathArray.length; j++) {
                String s = pathArray[j];
                if (currentMap.containsKey(s)) {
                    currentMap = currentMap.get(s);
                    continue;
                }
                currentMap.put(s, new HashMap());
                currentMap = currentMap.get(s);
            }
        }
        return result;
    }
}
